/**
 * 
 */
package org.ztest.executor;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @author lhl
 *
 * 2015年8月21日下午3:02:00
 */
public class MessageQueue
{
	private BlockingQueue<Message> mQueue;
	private boolean quitAllowed;
	
	public MessageQueue(boolean quitAllowed)
	{
		mQueue = new LinkedBlockingQueue<>();
		this.quitAllowed = quitAllowed;
	}
	
	Message next() throws InterruptedException
	{
		return mQueue.take();
	}
	
	boolean enqueueMessage(Message message)
	{
		return mQueue.offer(message);
	}
	
	void quit()
	{
		quit(true);
	}
	
	void quit(boolean safe)
	{
		if (!quitAllowed)
			throw new IllegalStateException("Quit is not allowed!");
		
		mQueue.clear();
	}
}
